import json
from pathlib import Path


def read_json_files_to_array(directory="data"):
    # 确保目录存在
    data_dir = Path(directory)
    if not data_dir.exists():
        print(f"目录 '{directory}' 不存在")
        return None

    json_array = []

    # 遍历目录下的所有JSON文件
    for file_path in data_dir.glob("*.json"):
        try:
            # 读取并解析JSON文件
            with open(file_path, "r", encoding="utf-8") as f:
                data = json.load(f)
                json_array.append(data)
                print(f"成功读取文件: {file_path}")
        except json.JSONDecodeError:
            print(f"警告: {file_path} 不是有效的JSON文件，已跳过")
        except Exception as e:
            print(f"读取 {file_path} 时出错: {str(e)}")

    return json_array
